package com.ficus.road.maintain.converter;

import com.ficus.road.maintain.config.DiseaseConfig;
import com.ficus.road.maintain.config.DiseaseConfigInfo;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.service.InspectService;
import com.ficus.road.maintain.service.ZhonghaiClient;
import com.ficus.road.maintain.util.BaiduMapClient;
import com.ficus.road.maintain.third.zh.roadgps.ZHRoadName;
import com.google.common.collect.Lists;
import ficus.car_demo.FrameMeta;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hao.zhou1
 * @date 2021/11/30 下午2:52
 */
@Component
public class MockHitConverter {
    @Autowired
    private DiseaseConfig diseaseConfig;
    @Autowired
    private InspectService inspectService;
    @Autowired
    private ZhonghaiClient zhonghaiClient;
    @Autowired
    private ZHRoadName zhRoadName;
    @Autowired
    private BaiduMapClient baiduMapClient;

    public Hit convert(MockHitCreateRequest mockHitCreateRequest) {
        Hit hit = new Hit();
        String type = mockHitCreateRequest.getType();
        hit.setType(type);
        long timestamp = System.currentTimeMillis();
        hit.setCreationTime(timestamp / 1000);
        hit.setLastModificationTime(timestamp / 1000);
        hit.setHitTime(mockHitCreateRequest.getHitTime());
        hit.setFirstHitTime(mockHitCreateRequest.getHitTime());
        DiseaseConfigInfo diseaseConfigInfo = diseaseConfig.getMap().get(type);

        int width = mockHitCreateRequest.getWidth() != null ? mockHitCreateRequest.getWidth().intValue() : 0;
        float length = mockHitCreateRequest.getLength() != null ? mockHitCreateRequest.getWidth() : 0;
        Float angle = mockHitCreateRequest.getAngle() != null ? mockHitCreateRequest.getAngle() : 0;
        Float area = mockHitCreateRequest.getArea() != null ? mockHitCreateRequest.getArea() : 0;
        Integer fallDownNumber = mockHitCreateRequest.getFallDownNumber() != null ? mockHitCreateRequest.getFallDownNumber() : 0;
        String roadType = mockHitCreateRequest.getRoadType() != null ? mockHitCreateRequest.getRoadType() : FrameMeta.RoadType.ROAD_TYPE_UNKNOWN.toString();
        Integer heightDiff = mockHitCreateRequest.getHeightDiff() != null ? mockHitCreateRequest.getHeightDiff().intValue() : 0;
        SeverityType severityType = mockHitCreateRequest.getSeverityType() != null ? mockHitCreateRequest.getSeverityType() : SeverityType.SEVERE;

        String carId = mockHitCreateRequest.getCarId();

        RelBbox relBbox = mockHitCreateRequest.getRelBbox();
        Map<String, String> carCompanyMap = inspectService.getCarCompanyMap();
        Map<String, String> companyNameMap = inspectService.getCompanyNameMap();
        String companyId = carCompanyMap.get(carId);
        String companyName = companyNameMap.get(companyId);
        GpsLocation gpsLocation = mockHitCreateRequest.getGpsLocation();
//        String roadInfo = zhonghaiClient.getRoadInfo(gpsLocation);
        String roadInfo = zhRoadName.getRoadName(gpsLocation);
        if (StringUtils.isBlank(roadInfo) || StringUtils.isBlank(roadInfo.split(",")[0])) {
            String streetName = baiduMapClient.geocoder(gpsLocation);
            roadInfo = new StringBuilder(streetName).append(StringUtils.isBlank(roadInfo) ? "" : roadInfo).toString();
        }
        // attrs
        HitAttrs hitAttrs = new HitAttrs();
        hitAttrs.setGpsLocation(gpsLocation);
        hitAttrs.setLocation(Lists.newArrayList(gpsLocation.getLongitude(), gpsLocation.getLatitude()));
        hitAttrs.setStatus(HitStatusEnum.DISPATCHED);
        hitAttrs.setCarId(carId);
        hitAttrs.setConfirmation(Confirmation.builder().status(
                Confirmation.ConfirmationStatus.CORRECT).confirmTime(new Date().getTime() / 1000).build());
        hitAttrs.setSceneImageName(StringUtils.substringAfter(mockHitCreateRequest.getSceneImage(), ".com/"));
        hitAttrs.setSceneImage(mockHitCreateRequest.getSceneImage());
        hitAttrs.setHandleCompanyCode(companyId);
        hitAttrs.setHandleCompany(companyName);
        hitAttrs.setRoadName(roadInfo);
        hitAttrs.setRoadType(roadType);
        hitAttrs.setSource(SourceEnum.MOCK.toString());
        hitAttrs.setIsTop(mockHitCreateRequest.getIsTop());

        if (diseaseConfigInfo != null) {
            hit.setClassification(diseaseConfigInfo.getClassification());
            hit.setScene(diseaseConfigInfo.getScene());
            hitAttrs.setLoop(diseaseConfigInfo.getLoop());
            hitAttrs.setLimitHour(diseaseConfigInfo.getLimitHour());
        }
        hit.setAttrs(hitAttrs);

        // subject
        HitSubject hitSubject = new HitSubject();
        hitSubject.setRelBbox(relBbox);
        hitSubject.setWidth(width);
        hitSubject.setLength(length);
        hitSubject.setArea(area);
        hitSubject.setAngle(angle);
        hitSubject.setHeightDiff(heightDiff);
        hitSubject.setFallDownNumber(fallDownNumber);
        hitSubject.setSeverityType(severityType);
        hitSubject.setType(type);
        hitSubject.setRoadType(roadType);
        hitSubject.setNeedPushOut(Boolean.TRUE);
        hitSubject.setHeadCount(0);
        hitSubject.setComfortRate(0f);
        hitSubject.setStatus(HitStatusEnum.DISPATCHED);
        hit.setSubjects(Lists.newArrayList(hitSubject));

        hit.setRoadType(roadType);
        hit.setAngle(angle);
        hit.setFallDownNumber(fallDownNumber);
        hit.setSeverityType(severityType);
        hit.setArea(area);
        hit.setLength(length);
        hit.setWidth(width);
        //historyList
        HistoryHit historyHit = new HistoryHit();
        historyHit.setType(type);
        historyHit.setHitTime(mockHitCreateRequest.getHitTime());
        historyHit.setSeverityType(severityType);
        historyHit.setWidth(width);
        historyHit.setAngle(angle);
        historyHit.setHeightDiff(heightDiff);
        historyHit.setLength(length);
        historyHit.setArea(area);
        historyHit.setFallDownNumber(fallDownNumber);
        historyHit.setHeadCount(0);
        historyHit.setComfortRate(0f);
        historyHit.setSubjects(Lists.newArrayList(hitSubject));
        HistoryHitAttrs historyHitAttrs = HistoryHitAttrs.builder().status(HitStatusEnum.NEW).carId(carId)
                .gpsLocation(gpsLocation).sceneImage(mockHitCreateRequest.getSceneImage()).build();
        historyHit.setAttrs(historyHitAttrs);
        historyHit.setRoadType(roadType);
        hit.setList(Lists.newArrayList(historyHit));
        return hit;
    }

    public MockHitDetailResponse parseHit(Hit hit) {
        MockHitDetailResponse mockHitDetailResponse = new MockHitDetailResponse();
        mockHitDetailResponse.setAngle(hit.getAngle());
        mockHitDetailResponse.setHitTime(hit.getFirstHitTime());
        mockHitDetailResponse.setIsTop(hit.getAttrs().getIsTop());
        mockHitDetailResponse.setWidth(hit.getWidth());
        mockHitDetailResponse.setType(hit.getType());
        mockHitDetailResponse.setSeverityType(hit.getSeverityType());
        List<HistoryHit> list = hit.getList();

        mockHitDetailResponse.setSceneImage(list.get(list.size() - 1).getAttrs().getSceneImage());
        mockHitDetailResponse.setRoadType(hit.getRoadType());
        mockHitDetailResponse.setRelBbox(list.get(list.size() - 1).getSubjects().get(0).getRelBbox());
        mockHitDetailResponse.setLength(hit.getLength());
        mockHitDetailResponse.setHeightDiff(hit.getHeightDiff());
        mockHitDetailResponse.setGpsLocation(hit.getAttrs().getGpsLocation());
        mockHitDetailResponse.setFallDownNumber(hit.getFallDownNumber());
        mockHitDetailResponse.setCarId(hit.getAttrs().getCarId());
        mockHitDetailResponse.setArea(hit.getArea());
        mockHitDetailResponse.setId(hit.getId());
        List<String> resolveImageUrls = hit.getAttrs().getResolveImageUrl();
        if (CollectionUtils.isNotEmpty(resolveImageUrls)) {
            mockHitDetailResponse.setResolveImage(resolveImageUrls.get(0));
            mockHitDetailResponse.setResolveTime(list.get(0).getHitTime());
        }
        return mockHitDetailResponse;
    }
}
